package com.easpeed.comon.utils

import android.app.Activity
import android.content.Context
import android.os.Build
import android.text.TextUtils
import android.view.View
import com.easpeed.elevator.R
import com.easpeed.elevator.utils.NoticeDialog
import java.util.*

class LoadingDialogUtils(var mContext: Context?) {

    private var timer: Timer? = null


    /**
     * 默认消失时间
     */
    private var dialogTimeOutMills = 30 * 1000L


    var noticeDialog: NoticeDialog? = null

    @JvmOverloads
    public fun show(overTimeCallback: ((mills: Long) -> Unit)? = null) {
        show("", overTimeCallback = overTimeCallback)
    }


    @JvmOverloads
    public fun show(msg: String?, overTimeMills: Long = dialogTimeOutMills, overTimeCallback: ((mills: Long) -> Unit)? = null) {
        val activity = getActivity(mContext)
        activity?.runOnUiThread {
            if (isFinish(activity)) {
                return@runOnUiThread
            }
            if (noticeDialog == null || noticeDialog?.isShowing != true) {
                noticeDialog =
                    NoticeDialog.Builder(mContext!!,  R.style.by_loading_dialog)
                        .setLayoutId(R.layout.by_dialog_loading)
                        .setCanceledOnTouchOutside(false)
                        .setText(R.id.tips, msg)
                        .show()
                noticeDialog?.getView(R.id.tips)?.visibility =
                    if (TextUtils.isEmpty(msg)) View.GONE else View.VISIBLE
                noticeDialog?.setOnDismissListener {
                    timer?.cancel()
                    timer = null
                }
                dismiss(overTimeMills, overTimeCallback = overTimeCallback)
            } else {
                dismiss(overTimeMills, overTimeCallback = overTimeCallback)
            }
        }
    }

    public fun showFull(overTimeCallback: ((mills: Long) -> Unit)? = null) {
        val activity = getActivity(mContext)
        activity?.runOnUiThread {
            if (isFinish(activity)) {
                return@runOnUiThread
            }
            if (noticeDialog == null || noticeDialog?.isShowing != true) {
                noticeDialog =
                    NoticeDialog.Builder(mContext!!,  R.style.by_loading_dialog)
                        .setLayoutId(R.layout.by_dialog_loading)
                        .setHideBottomLine(true)
                        .setCanceledOnTouchOutside(false)
                        .show()
                noticeDialog?.getView(R.id.tips)?.visibility = View.GONE
                noticeDialog?.setOnDismissListener {
                    timer?.cancel()
                    timer = null
                }
                dismiss(dialogTimeOutMills, overTimeCallback = overTimeCallback)
            } else {
                dismiss(dialogTimeOutMills, overTimeCallback = overTimeCallback)
            }
        }
    }

    /**
     * 默认消失时间
     */
    public fun setDefaultHideTime(timeOut: Long) {
        this.dialogTimeOutMills = timeOut
    }

    public fun dismiss() {
        try {
            timer?.cancel()
            timer = null
            val activity = getActivity(mContext)
            activity?.runOnUiThread {
                if (noticeDialog?.isShowing == true && !isFinish(activity)) {
                    noticeDialog?.dismiss()
                    noticeDialog = null
                }
            }
        } catch (e: Exception) {
        }
    }

    private fun isFinish(activity: Activity?): Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (activity?.isDestroyed == true || activity?.isFinishing == true) {
                return true
            }
        } else {
            if (activity?.isFinishing == true) {
                return true
            }
        }
        return false
    }

    private fun getActivity(mContext: Context?): Activity? {
        if (mContext == null) {
            return null
        }
        return mContext as Activity
    }

    @Synchronized
    @JvmOverloads
    public fun dismiss(delay: Long, overTimeCallback: ((mills: Long) -> Unit)? = null) {
        timer?.cancel()
        timer = null
        try {
            if (timer == null && noticeDialog?.isShowing == true) {
                timer = Timer()
                timer?.schedule(object : TimerTask() {
                    override fun run() {
                        dismiss()
                        timer = null
                        overTimeCallback?.invoke(delay)
                    }
                }, delay)
            }
        } catch (e: Exception) {
        }
    }

//    fBLQw57mp3Q:APA91bHJ_ezWq4O0D3jysKXAuGVJWbJ3ynF8XFRCJxbqGvFjKTXhUl0RsCVWUNmu09A8IxZkgDjS22v02sMu_NsnGjFeJ5equKwkgbDmnXoJAZOSvK-dw94-h5kv8_XHS_3V4ezLbbKk
//    dI3_bUVFuzI:APA91bGbepcXhtxjpXXgkujZmOPAiCezZQnHHD7wL7YWrLI_IAOklO015s3qYr9YHUSue2Iyp0b0wONF0wRLa4rUZwRWo3nPPENrHbR_pp-v21MUx0abZ6wTYcXNUQJf61F-fq3lAMWP
}